package cn.crc.web.net;

import org.apache.ftpserver.ConnectionConfig;
import org.apache.ftpserver.ConnectionConfigFactory;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务端FTP工具_25.4.25_liuhz
 */
public class TestFTPServer {
    public static void main(String[] args) {
        TestFTPServer.createFTPServer();
    }

    public static void createFTPServer(){
        String ftpusername= ResourceUtils.getString("ftpserver.username");
        String ftppassword= ResourceUtils.getString("ftpserver.password");
        int ftpport= ResourceUtils.getInt("ftpserver.port");
        String ftphome= ResourceUtils.getString("ftpserver.home");

        List<Authority> listAuth= new ArrayList<>();
        listAuth.add(new WritePermission());
        BaseUser baseUser= new BaseUser();
        baseUser.setName(ftpusername);
        baseUser.setPassword(ftppassword);
        baseUser.setHomeDirectory(ftphome);
        baseUser.setAuthorities(listAuth);

        ConnectionConfigFactory configFactory = new ConnectionConfigFactory();// 连接管理器
        configFactory.setMaxLogins(1000);
        ConnectionConfig connectionConfig = configFactory.createConnectionConfig();
        ListenerFactory listenerFactory= new ListenerFactory();
        listenerFactory.setPort(ftpport);

        FtpServerFactory serverFactory= new FtpServerFactory();
        serverFactory.addListener("default", listenerFactory.createListener());
        serverFactory.setConnectionConfig(connectionConfig);
        try {
            serverFactory.getUserManager().save(baseUser);
            FtpServer ftpServer= serverFactory.createServer();
            ftpServer.start();
        } catch (FtpException e) {
            e.printStackTrace();
        }
    }

    private static void startFtpServer() {
        FtpServerFactory serverFactory = new FtpServerFactory();
        ListenerFactory factory = new ListenerFactory();
        factory.setPort(ResourceUtils.getInt("ftpserver.port"));
        serverFactory.addListener("default", factory.createListener());

        BaseUser user = new BaseUser();
        user.setName(ResourceUtils.getString("ftpserver.username"));
        user.setPassword(ResourceUtils.getString("ftpserver.password"));
        user.setHomeDirectory(ResourceUtils.getString("ftpserver.home"));
        List<Authority> authorities = new ArrayList<Authority>();
        authorities.add(new WritePermission());
        user.setAuthorities(authorities);

        ConnectionConfigFactory connectionConfigFactory = new ConnectionConfigFactory();// 连接管理器
        connectionConfigFactory.setMaxLogins(1000);
        ConnectionConfig connectionConfig = connectionConfigFactory.createConnectionConfig();
        serverFactory.setConnectionConfig(connectionConfig);
        try {
            serverFactory.getUserManager().save(user);
            FtpServer server = serverFactory.createServer();
            server.start();
            System.out.println("FTP启动成功");
        } catch (FtpException e) {
            System.out.println(e.getMessage());
        }
    }
    //protected static Logger log = LoggerFactory.getLogger(TestFTPServer.class);
}
